<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
<head>
<meta charset="UTF-8">
<title>我要投资-余额宝详情|购买</title>
<jsp:include page="/include/wchat/head.jsp"></jsp:include>

<link type="text/css" rel="stylesheet" href="/v2/wchat/resrc/css/invest.css">
</head>
<body>
<div id="header"> <a href="/wchatYebDetail.do?pid=${productDetailMap.pid}" class="backto">返回</a>
  <div class="title-sub">余额宝详情</div>
</div>
<div id="wrap">
  <div class="invest-detail"><b class="icon-qiang"></b>
    <h1>
      
      
            余额宝 ${productDetailMap.SN }
      
    </h1>
    <ul class="items">
      <li class="c1">可购金额</li>
      <li class="c2">￥${productDetailMap.remain}</li>
    </ul>
    <ul class="items">
      <li class="c1">账户余额</li>
      <li class="c2">￥<s:if test="#request.accmountStatisMap.usableSum !=''">${accmountStatisMap.usableSum}</s:if><s:else>0</s:else>
      <input type="hidden" id="realnameFlag" value="${session.user.isBindIdNo}">
      </li>
    </ul>
    <s:if test="#request.productDetailMap.remain > 100000" >
	    <ul class="items">
	      <li class="c1">单户最多可投</li>
	      <li class="c2 safe">￥100,000元</li>
	    </ul>
    </s:if>
    <ul class="items">
      <li class="c1">安全保障</li>
      <li class="c2 safe">100%本息保障</li>
    </ul>
  </div>
  <div class="invest-buy">
    <ul>
      <li class="money">
        <label class="txt">购买金额</label>
        <input class="input" id="inputAmount" maxlength="8" placeholder="请输入1000的整数倍数">
      </li>
      
      <li class="verify"> <span class="verify-code">
        <input type="text" id="authCode" class="input" maxlength="4" placeholder="请输入验证码">
        </span><img src="I4008lOve893gOfUll983/imageCode.do?pageId=yebWchatInvest" onclick="coderefresh()" id="verifyCodeImg"> </li>
      <li class="argee">
        <input type="checkbox" id="agreeContract" class="ipt-chk" checked="checked">
        我同意《<a href="/yebContractDetail.do">共富网余额宝投资协议</a>》</li>
    </ul>
    <div class="vtip" id="errorTip"></div>
  </div>
  <div class="operate-button operate-buy"> 
  	<a href="javascript:invest();" class="btn" id="investBt">我要购买</a> 
  	<a href="/wchatRealNameAuthen.do" class="btn" id="realNameBt" style="display: none;">立即实名认证</a> 
  </div>
</div>
<jsp:include page="/include/wchat/footer.jsp"></jsp:include>
<script type="text/javascript">
var product = {
		aunualInterestRate : '${productDetailMap.productAnnualRate}',
		remain : '${productDetailMap.remain}',
		id : '${productDetailMap.pid}'
	};
environment.userCash = '${accmountStatisMap.usableSum}';
$(function(){
	/**
	var loanStatus = '300';
	if (loanStatus != "300") {
     window.location.href="/yuexitong/loanDetail.do?loanId=45185";
	}
	**/
  $('#agreeContract').click(function(){
    if ($('#agreeContract').attr("checked") == "checked") {
      $('#agreeContract').removeAttr("checked");
    } else {
      $('#agreeContract').attr("checked", "checked");
    }
  });
});
//刷新验证码
function coderefresh() {
    var verifyCode = $("#verifyCodeImg");
    var now = new Date();
    verifyCode.attr("src",'I4008lOve893gOfUll983/imageCode.do?pageId=yebWchatInvest&d=' + now.getTime());
}
//投资检查
function investCheck() {
	var errorTip = $('#errorTip');
	var cash = Number(environment.userCash);
    var investAmountInput = $("#inputAmount");
    var investAmount = investAmountInput.val();
    var maxInvestAmount = Number(product.remain);
    var investableCash = Number(environment.userCash);
    var availableCouponAmount = Number($("#availableCouponAmount").html());
    if(isNaN(investAmount*1) || investAmount*1<=0){
        errorTip.html('请输入正确的金额');
        return false;
    }
    if(investAmount%1000 != 0){
        errorTip.html('投资额只能为1000的整数倍');
        return false;
    }
    if(investAmount > maxInvestAmount){
        errorTip.html('投资额不能大于剩余可投额度');
        return false;
    }
    /**
    if(investAmount > 10000.00){
        Util.arrowPrompt($("#investableAmount"),'投资额不能大于单笔最高可投额度', 'err');
        return false;
    }
    **/
    if(investAmount > cash){
        errorTip.html('您的余额不足，请充值');
        return false;
    }
    /**
    var useCoupon = $("#useCoupon");
    if(useCoupon.hasClass("selected")){
        if(investAmount - availableCouponAmount > cash){
            errorTip.html('您的余额不足，请充值');
            return false;
        }
    }else{
        if(investAmount - availableCouponAmount > cash){
            errorTip.html('您的余额不足，请充值');
            return false;
        }else if(investAmount > cash && investAmount <= (cash + availableCouponAmount)){
            errorTip.html('您的余额不足，可使用红包');
            return false;
        }
    }**/
    if(investAmount < 1000.00){
        errorTip.html('最小投资金额为'+1000.00);
        return false;
    }

    var verifyCodeInput = $("#authCode");
    var verifyCode = verifyCodeInput.val();
    if(verifyCode == ''){
        errorTip.html('请输入验证码');
        return false;
    }

    if($('#agreeContract').attr("checked") != "checked"){
        errorTip.html('您尚未同意相关投资协议');
        return;
     }
    
    //未实名认证，不能投资
	if($("#realnameFlag").val() == "0"){
        errorTip.html('未实名认证，不能投资');
        $('#realNameBt').show();
		$('#investBt').hide();
        return false;
    }
    return true;
}
//投资月息通或新手标
function invest() {
  if(!investCheck()){
    return;
  };
  var errorTip = $('#errorTip');
  var investAmount = $("#inputAmount").val();
  var verifyCode = $("#authCode").val();
  
  var param = {};
  param['paramMap.pid'] = product.id;
  param["paramMap.pageId"] = "yebWchatInvest";
  param["paramMap.code"] = verifyCode;
  param['paramMap.amount'] = investAmount;
  
  $.ajax({
          dataType: 'json',
          url: environment.basePath+"/yebFinanceInvest.do",
          data: param,
          type: 'POST',
          beforeSend: function() {
              $("#investBt").attr("href","javascript:;").html("正在加入...").addClass("gbtn-disabled");
          },
          success: function(data) {
              if(data.result == 'success'){
            	  alert("恭喜您，投资成功！");
                  window.location.href="/wchatYebDetail.do?pid="+${productDetailMap.pid};
              }else{
                  if(data.msg == '验证码错误'){
                      coderefresh();
                	  errorTip.html("验证码错误");
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }else{
                	  coderefresh();
                	  errorTip.html(data.msg);
                      $("#investBt").attr("href","javascript:invest();").html("我要购买").removeClass("gbtn-disabled");
                  }
              }
          },
          error: function() {
        	  coderefresh();
        	  errorTip.html("投资失败，请刷新页面或稍后再试");
          }
      });

}
//计算可使用红包金额
function calAvailableCouponAmount(){
    var inputAmount = Number($("#inputAmount").val());
    var availableCouponAmount = $(".availableCouponAmount");
    for(var i=0,len=availableCouponAmount.length;i<len;i++){
        var leftAmount = Number($(availableCouponAmount[i]).attr("leftamount"));
        var usageType = Number($(availableCouponAmount[i]).attr("usagetype"));
        var discountRatio = Number($(availableCouponAmount[i]).attr("discountratio"));
        var discountAmount = Number($(availableCouponAmount[i]).attr("discountamount"));
        var couponId = $(availableCouponAmount[i]).attr("couponid");
        var typeTitle = $(availableCouponAmount[i]).attr("typetitle");
        var useCoupon=$('#coupon' + couponId);
        if(usageType == 800){
            var availableAmount = inputAmount*discountRatio;
            if(availableAmount > leftAmount){
              availableAmount = leftAmount;
            }
        }else{
            var availableAmount = leftAmount > discountAmount ? discountAmount : leftAmount;
        }
       
        if(isNaN(availableAmount)){
            availableAmount = 0;
        }
        useCoupon.html(typeTitle + "￥" + availableAmount.toFixed(2));
        useCoupon.attr("useCouponAmount", availableAmount.toFixed(2));
    }
}
</script>
</body>
</html>